package com.tron.sync.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tron.sync.commons.BaseController;
import com.tron.sync.entities.JZOrg;
import com.tron.sync.entities.JZUser;
import com.tron.sync.service.JZOrgService;
import com.tron.sync.service.JZServiceImpl;
import com.tron.sync.service.JZUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@Api(tags = "警综平台用户")
@RestController
public class JZController extends BaseController {

    @Autowired
    JZServiceImpl jzService;

    @Autowired
    JZUserService jzUserService;

    @Autowired
    JZOrgService jzOrgService;

    /**
     * edit 2
     * 从警宗平台把用户同步到本地库
     */

    @ApiOperation(value = "同步警综平台用户到本地数据库")
    @GetMapping("/JZ/syncUser")
    public String syncUser() {
        int countUser1 = 0;
        int countUser2 = 0;
        int page = 1;
        int size = 0;
        do {
            //获取辅警,分页递增，直到取不到数据为止。
            List<JZUser> jzUserList = jzService.getUsers01(page);
            size = jzUserList.size();
            countUser1 += size;
            jzUserService.saveBatch(jzUserList);
            page ++;
        } while (size != 0);
        page = 1;
        do {
            //获取民警，分页递增，直到取不到数据为止。
            List<JZUser> jzUserList = jzService.getUsers02(page);
            size = jzUserList.size();
            countUser2 += size;
            jzUserService.saveBatch(jzUserList);
            page ++;
        } while (size != 0);

        return "同步成功：辅警总数-"+countUser1+" 民警总数："+countUser2;
    }

    @ApiOperation(value = "同步警综平台机构到本地数据库")
    @GetMapping("/JZ/syncOrg")
    public String syncOrg() {
        int count = 0;
        int size = 0;
        int page = 1;
        do {
            List<JZOrg> org = jzService.getOrg(page);
            size = org.size();
            count += size;
            jzOrgService.saveBatch(org);
            page++;
        } while (size != 0);
        return "同步成功： 机构总数："+count;
    }

    @ApiOperation(value = "查看警综平台已经同步到本地的用户")
    @GetMapping("/JZ/getUser")
    public Result<List<JZUser>> getUser(Integer page, Integer size) {
        Page<JZUser> myPage = new Page<>(page, size);
        Page<JZUser> userPage = jzUserService.page(myPage);
        long total = userPage.getTotal();
        List<JZUser> records = userPage.getRecords();
        return buildResult(records, page, size, total);
    }

    @ApiOperation(value = "查看警综平台已经同步到本地的机构")
    @GetMapping("/JZ/getOrg")
    public Result<List<JZOrg>> getOrg(Integer page, Integer size) {
        Page<JZOrg> myPage = new Page<>(page, size);
        Page<JZOrg> orgPage = jzOrgService.page(myPage);
        long total = orgPage.getTotal();
        List<JZOrg> records = orgPage.getRecords();
        return buildResult(records, page, size, total);
    }


    @ApiOperation(value = "删除重复身份证用户")
    @GetMapping("/JZ/deleteRepetitiousUser")
    public String deleteRepetitiousUser() {
        List<JZUser> list = jzUserService.list();
        ArrayList<JZUser> collect = list.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<JZUser>(Comparator.comparing(o -> o.getCardNo()))), ArrayList::new
        ));
        jzUserService.removeByIds(list.stream().map(JZUser::getId).collect(Collectors.toList()));
        jzUserService.saveBatch(collect);
        return "去重前数据："+list.size() + " 去重后数据： "+ collect.size();
    }

    @ApiOperation(value = "批量设置UUID")
    @GetMapping("/setUUID")
    public Integer setUUID() {
        List<JZUser> list = jzUserService.list();
        list.forEach(e->{
            e.setUuid(UUID.randomUUID().toString());
        });
        jzUserService.updateBatchById(list);
        return list.size();
    }

}
